package com.qyhl.webtv.module_user.login.code;

import com.orhanobut.hawk.Hawk;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.base.BaseModel;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.AppConfigConstant;
import com.qyhl.webtv.commonlib.entity.user.LoginFirstBean;
import com.qyhl.webtv.commonlib.entity.user.UserBean;
import com.qyhl.webtv.commonlib.entity.user.UserInfoBean;
import com.qyhl.webtv.commonlib.utils.config.AppConfigUtil;
import com.qyhl.webtv.module_user.app.UserApplication;
import com.qyhl.webtv.module_user.common.UserUrl;
import com.qyhl.webtv.module_user.util.LoginUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class CodeLoginPresenterModel extends BaseModel implements CodeLoginContract.CodeLoginPresenterModel {


    private CodeLoginContract.CodeLoginView view;

    public CodeLoginPresenterModel(CodeLoginContract.CodeLoginView view) {
        super();
        this.view = view;
    }

    @Override
    public void getCode(String phone) {
        if (NetUtil.isNetworkAvalible(UserApplication.getApp())) {
            Map<String, String> map = DESedeUtil.encodeMapUrl(UserUrl.GET_USER_GETCODE);

            EasyHttp.post(map.get(DESedeUtil.URL))
                    .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY),
                            "siteId=" + CommonUtils.getInstance().getSite() + "&type=3&telphone=" + phone))
                    .isDecrypt(true)
                    .decryptKet(map.get(DESedeUtil.CODEKEY))
                    .execute(new SimpleCallBack<String>() {
                        @Override
                        public void onError(ApiException e) {
                            view.getCodeError("获取验证码失败!");
                        }

                        @Override
                        public void onSuccess(String s) {
                            view.getCodeSuccess();
                        }
                    });

        } else {
            view.getCodeError("请检查网络...");
        }
    }

    @Override
    public void isUserExist(final String phone, final String code) {
        if (NetUtil.isNetworkAvalible(UserApplication.getApp())) {

            EasyHttp.post(UserUrl.GET_USER_CODEFIRST)
                    .params("username",phone)
                    .execute(new SimpleCallBack<LoginFirstBean>() {
                        @Override
                        public void onError(ApiException e) {
                            view.userExistError(LoginUtils.getMessage(e.getCode()));
                        }

                        @Override
                        public void onSuccess(LoginFirstBean loginFirstBean) {
                            if (loginFirstBean.isExist()) {
                                goLogin(phone, code, "", "", "");
                            } else {
                                //这个地方如果没有注册且验证码错误，后台是返回的没注册，应该优先返回验证码错误的
                                view.userNoExist();
                            }
                        }
                    });

        } else {
            view.userExistError("请检查网络...");
        }
    }

    private void goLogin(String phone, String code, String nickName, String invitecode, String password) {

        if (NetUtil.isNetworkAvalible(UserApplication.getApp())) {
            Map<String, String> map = DESedeUtil.encodeMapUrl(UserUrl.CODE_USER_LOGIN);
            EasyHttp.post(map.get(DESedeUtil.URL))
                    .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY),
                            "siteId=" + CommonUtils.getInstance().getSite() + "&captcha=" + code + "&telphone=" + phone + "&nickname=" +
                                    nickName + "&code=" + invitecode + "&password=" + password))
                    .isDecrypt(true)
                    .decryptKet(map.get(DESedeUtil.CODEKEY))
                    .execute(new SimpleCallBack<UserBean>() {
                        @Override
                        public void onError(ApiException e) {
                            view.loginError(LoginUtils.getMessage(e.getCode()));
                        }

                        @Override
                        public void onSuccess(UserBean userBean) {
                            AppConfigUtil.getInstance().setConValue(AppConfigConstant.LOGIN_TOKEN,userBean.getToken());
                            Hawk.put("token", userBean.getToken());
                            getUserInfo(userBean.getUsername());
                        }
                    });
        } else {
            view.loginError("请检查网络...");
        }
    }




    /**
     * 获取用户信息
     *
     * @param username
     */
    private void getUserInfo(String username) {
        if (NetUtil.isNetworkAvalible(UserApplication.getApp())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String time = sdf.format(new Date());
            String code = "Z5" + time + "I2863";
            String url = UserUrl.GET_USER_INFO_NEW + "?timestamp=" + time;

            EasyHttp.post(url)
                    .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&username=" + username))
                    .isDecrypt(true)
                    .decryptKet(code)
                    .execute(new SimpleCallBack<UserInfoBean>() {
                        @Override
                        public void onError(ApiException e) {
                            view.loginError("网络繁忙，请稍后再试");
                        }

                        @Override
                        public void onSuccess(UserInfoBean userInfoBean) {
                            view.loginSuccess(userInfoBean);
                        }
                    });
        } else {
            view.loginError("请检查网络...");
        }
    }

}
